/*
全局类，主要包括一些常用的方法
*/
window.protectedUnschedule = function(node,schedule){
    if(schedule == -1)
        return;
    if(schedule == false)
        return;
    node.unschedule(schedule);
};
window.DKTools = {
    //退出游戏,不同平台需要不同处理
    exitGame : function(){
        cc.director.end();
        if(cc.sys.platform == cc.sys.MOBILE_BROWSER)
            window.close()
        if(cc.sys.platform == cc.sys.IPHONE || cc.sys.platform == cc.sys.IPAD)
            jsb.reflection.callStaticMethod("JSCallOC","GameExit");
    },
    //音乐音效模块
    audioEG : {
        audioBgId : -1,
        audioList : {},
        curBgName : "",
        audioTimeId:-1,
        curTimeName:"",
        //播放背景音乐
        playBg : function(bgmName){
            if(local.audioEG.curBgName == bgmName)
                return;
            local.audioEG.stopBg();
            // if(window.localSetting.gameData.musicEnable){
                local.audioEG.audioBgId = cc.audioEngine.play(local.audioEG.audioList[bgmName],true,window.localSetting.gameData.musicValue);
                local.audioEG.curBgName = bgmName;
            // }
        },
        //暂停背景音乐
        stopBg : function(){
            if(local.audioEG.audioBgId != -1){
                cc.audioEngine.stop(local.audioEG.audioBgId);
                local.audioEG.audioBgId = -1;
                local.audioEG.curBgName = "";
            }
        },
        //播放音效
        playEffect:function(name){
            console.log("playEffect===="+name);
            console.log("soundValue===="+window.localSetting.gameData.soundValue);
            // if(window.localSetting.gameData.soundEnable){
                if(local.audioEG.audioList[name]){
                    if(name == "tips_timer10s")
                    {
                        local.audioEG.stopTimerEffect();
                        local.audioEG.audioTimeId = cc.audioEngine.play(local.audioEG.audioList[name],false,window.localSetting.gameData.soundValue);
                        local.audioEG.curTimeName = name;
                    }
                    else
                    {
                        cc.audioEngine.play(local.audioEG.audioList[name],false,window.localSetting.gameData.soundValue);
                    }
                }else{
                    console.log("playEffect audioList name==== false");
                }
            // }else{
                // console.log("playEffect soundEnable ==== false");
            // }
        },
        //停止播放计时器声音
        stopTimerEffect:function()
        {
            if(local.audioEG.audioTimeId != -1){
                cc.audioEngine.stop(local.audioEG.audioTimeId);
                local.audioEG.audioTimeId = -1;
                local.audioEG.curTimeName = "";
            }
        }
    },
    //对数组进行快速排序的方法,传入一个对象数组arr和参数type,用arr[i]元素的type参数进行降序排序(一般用于对记录进行按时间参数排序);
    quickSort : function(arr,type){
        for(var i=0;i<arr.length-1;i++){
            for(var j=0;j<arr.length-1-i;j++){
                // console.log(arr[j])
                // console.log(type)
                // console.log(arr[j][type])
                if(arr[j][type]<arr[j+1][type]){
                    var temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        return arr;
    },

    //将下注总和分割成下注数组
    cutBetNum : function(num) {
        var type = [20,15,10,5,1]
        var arr = []
        for(var i = 0;i < type.length;i++){
            var count = Math.floor(num / type[i])
            num = num % type[i]
            for(var j = 0;j < count;j++){
                arr.push(type[i])
            }
        }
        return arr
    },

    //获取当前时间戳转换后的总天数
    getDayCount : function(){
        var newDate = Date.parse(new Date());
        var curDayCount = parseInt(newDate/1000/86400);
        return curDayCount;
    },

    //获取当前的年月日,格式:2017-12-6
    getDateDay : function(time){
        var curDate = new Date();
        if(time)
            curDate = new Date(time);
        var dayString = curDate.getFullYear()+"-"+(parseInt(curDate.getMonth())+1)+"-"+curDate.getDate();
        return dayString;
    },

    //获取当前的时分秒,格式:16:32:13
    getDateTime : function(time){
        var curDate = new Date();
        if(time)
            curDate = new Date(time);
        var timeString = curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds();
        return timeString;
    },

    //下载图片再设置到指定的精灵贴图上,常用于设置头像
    setFrameWithUrl : function(headUrl,sprite) {
        if(headUrl == ""){
            console.log("url ===== NULL");
            return;
        }
        cc.loader.load({url:headUrl,type:'png'}, function (err, texture) {
            var newFrame = new cc.SpriteFrame(texture);
            if(sprite)
                sprite.spriteFrame = newFrame;
        });
    }
};

var local = window.DKTools;